package com.lxy.cxcyxmmsbackend.sys.service.impl;

import com.lxy.cxcyxmmsbackend.sys.entity.QxglCd;
import com.lxy.cxcyxmmsbackend.sys.entity.QxglDz;
import com.lxy.cxcyxmmsbackend.sys.exceptionHandle.exce.NormalException;
import com.lxy.cxcyxmmsbackend.sys.mapper.PermissionMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglCdMapper;
import com.lxy.cxcyxmmsbackend.sys.mapper.QxglDzMapper;
import com.lxy.cxcyxmmsbackend.sys.res.PermissionRelation;
import com.lxy.cxcyxmmsbackend.sys.res.Result;
import com.lxy.cxcyxmmsbackend.sys.service.PermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionServiceImpl implements PermissionService {
    @Resource
    QxglCdMapper menuMapper;
    @Resource
    QxglDzMapper actionMapper;
    @Resource
    PermissionMapper permissionMapper;

    @Override
    @Transactional
    public Result selectAllPermission() {
        //结果类列表  一级菜单id label children=>二级菜单id label children=>菜单下动作 id label children(null)
        ArrayList<PermissionRelation> permissionRelations = new ArrayList<>();
        List<QxglCd> menus = menuMapper.selectQxglCds();
        for (QxglCd menu : menus) {
            PermissionRelation permissionRelation1 = new PermissionRelation();
            //查询一级菜单 赋值 id label
            permissionRelation1.setId(menu.getCdBh());
            permissionRelation1.setLabel(menu.getCdMc());
            //判断一级菜单是否有动作
            List<QxglDz> actions1 = actionMapper.selectQxglDzByCdBh(menu.getCdBh());
            if (actions1 == null || actions1.size() == 0) {
                //遍历一级菜单 子菜单列表 赋值第二层 id label
                if (menu.getChildren() != null && menu.getChildren().size() != 0) { //判断存在子菜单
                    for (QxglCd child : menu.getChildren()) {
                        PermissionRelation permissionRelation2 = new PermissionRelation();
                        permissionRelation2.setId(child.getCdBh());
                        permissionRelation2.setLabel(child.getCdMc());
                        List<QxglDz> actions = actionMapper.selectQxglDzByCdBhPer(child.getCdBh());
                        if (actions != null && actions.size() != 0) {
                            for (QxglDz action : actions) {
                                PermissionRelation permissionRelation3 = new PermissionRelation();
                                //遍历二级菜单 查询菜单下动作 赋值第三层的id label
                                permissionRelation3.setId(action.getDzBh());
                                permissionRelation3.setLabel(action.getDzMc());
                                //第三层动作列表赋值给第二层二级菜单的children
                                permissionRelation2.getChildren().add(permissionRelation3);
                            }
                        }
                        //第二层列表赋值给 一级菜单children
                        permissionRelation1.getChildren().add(permissionRelation2);
                    }
                }
            } else {
                for (QxglDz action : actions1) {
                    PermissionRelation permissionRelation3 = new PermissionRelation();
                    //遍历一级菜单下动作 查询菜单下动作 赋值第二层的id label
                    permissionRelation3.setId(action.getDzBh());
                    permissionRelation3.setLabel(action.getDzMc());
                    //第三层动作列表赋值给第二层二级菜单的children
                    permissionRelation1.getChildren().add(permissionRelation3);
                }

            }


            permissionRelations.add(permissionRelation1);
        }

        System.out.println(permissionRelations);
        return Result.success("查询菜单动作关系成功", permissionRelations);
    }

    @Override
    @Transactional
    public Result selectMIdByRId(String roleBh) {
        List<String> integers = permissionMapper.selectMIdByRId(roleBh);
        return Result.success("查询菜单编号成功", integers);
    }

    @Override
    @Transactional
    public Result selectAIdByRId(String roleBh) {
        List<String> integers = permissionMapper.selectAIdByRId(roleBh);
        return Result.success("查询菜单编号成功", integers);
    }

    @Override
    public Result selectMIdAIdByRId(String roleBh) {
        List<String> menu_ids = permissionMapper.selectMIdByRId(roleBh);
        List<String> action_nos = permissionMapper.selectAIdByRId(roleBh);
        menu_ids.addAll(action_nos);
        return Result.success("查询菜单编号和动作编号成功", menu_ids);
    }

    //测试： 无此菜单 新增此菜单下动作  同时为该角色添加此菜单权限  √
    //      新增该角色不拥有的菜单时 同时为其添加改菜单下的普通动作  √
    //      新增已有菜单下的动作  √
    //      删除此菜单的同时删除该角色拥有的该菜单下的动作权限（普通+授权）  √
    //      删除已有菜单下的动作 √
    @Override
    @Transactional
    public Result empowered(String roleBh, String perId) {
        //赋权
        if (String.valueOf(perId).startsWith("A")) { //动作
            //判断后否具有角色动作权限
            int i1 = permissionMapper.selectRARel(roleBh, perId);
            if (i1 == 0) { //无动作权限
                //判断该角色是否拥有该动作的菜单
                QxglDz action = actionMapper.selectActionById(perId);
                int i2 = permissionMapper.selectRMRel(roleBh, action.getDzSscd());
                if (i2 == 0) {
                    int i = permissionMapper.insertRmRel(roleBh, action.getDzSscd());
                    if (i != 1) {
                        throw new NormalException("新增该动作菜单权限失败");
                    }
                }
                //新增角色动作关系表记录
                int i3 = permissionMapper.insertRARel(roleBh, perId);
                if (i3 != 1) {
                    throw new NormalException("新增动作权限失败");
                }
            } else if (i1 == 1) {//具有动作权限
                //删除该角色动作关系表记录
                int i = permissionMapper.deleteRARelOne(roleBh, perId);
                if (i != 1) {
                    throw new NormalException("删除动作权限失败");
                }
            } else { //异常
                throw new NormalException("角色动作权限重复");
            }

        } else if (String.valueOf(perId).startsWith("M")) { //菜单
            int i1 = permissionMapper.selectRMRel(roleBh, perId);
            if (i1 == 0) { //无菜单权限
                //新增角色菜单关系表记录
                int i = permissionMapper.insertRmRel(roleBh, perId);
                //获取菜单下的普通动作，添加到角色动作表中
                List<QxglDz> actions = actionMapper.selectQxglDzByCdBhCom(perId);
                if (actions!=null&&actions.size()>0){
                    for (QxglDz action : actions) {
                        int i3 = permissionMapper.insertRARel(roleBh, action.getDzBh());
                        if (i3 != 1) {
                            throw new NormalException("新增普通动作"+action.getDzMc()+"权限失败");
                        }
                    }
                }
                if (i != 1) {
                    throw new NormalException("新增菜单权限失败");
                }
            } else if (i1 == 1) { //有该菜单权限
                //删除该角色菜单关系表记录
                int i = permissionMapper.deleteRmRelOne(roleBh, perId);
                if (i != 1) {
                    throw new NormalException("删除菜单权限失败");
                }

                //获取此菜单的动作
                List<QxglDz> actions = actionMapper.selectQxglDzByCdBh(perId);
                if (actions!=null&&actions.size()>0){ //删除此菜单下的动作
                    //查询该角色的拥有的所有动作
                    List<String> allAction_no = permissionMapper.selectAIdByRId(roleBh);
                    //判断该动作是否被该角色拥有

                    for (QxglDz action : actions) {
                        if (allAction_no.contains(action.getDzBh())){
                            int i3 = permissionMapper.deleteRARelOne(roleBh, action.getDzBh());
                            if (i3 != 1) {
                                throw new NormalException("删除角色动作"+action.getDzMc()+"权限失败");
                            }
                        }

                    }
                }

            } else {
                throw new NormalException("角色菜单权限重复");
            }
        }
        return Result.success("修改权限成功", perId);
    }


}
